package com.sx.micowell.framework.model

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.example.mvvm.base.data.SingleLiveEvent
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.rx.RxBus

import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.SPUtils
import com.example.mvvm.until.ToastUtils
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.service.IApiService
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.TokenVO
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath

import com.sx.micowell.framework.binding.BindingCommand
import com.sx.micowell.framework.binding.impl.IBindingAction
import com.sx.micowell.framework.tools.CacheUtils
import com.sx.micowell.framework.tools.EventInfo
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.HashMap

/**
 * @Author: YY
 * @Time: 2021/8/13
 * @Describe TODO
 */
class SetupPwdModel : BaseViewModel() {

    override fun registerRxBus() {
        super.registerRxBus()
        addSubscribe(
            RxBus.getDefault().toObservable(EventInfo::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { event ->
                    if (event.type == AppConstant.EVENT_REGISTER_PWD_SUCCESS) {
                        // 注册成功、修改密码成功
                        context.finishEvent.call()
                    }
                }
        )
    }


    var type : Int = 0
    // 手机号
    private var phoneNumField = ObservableField("")
    // 验证码
    private var verifyCodeField = ObservableField("")
    // 密码
    var pwdField = ObservableField("")
    // 确认密码
    var confirmPwdField = ObservableField("")

    // 密码开关
    var pwdSwitchEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    // 确认密码显示
    var confirmPwdSwitchEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()

    //密码显示开关
    var pwdShowSwitchOnClickCommand: BindingCommand<Boolean> = BindingCommand { isChecked ->
        pwdSwitchEvent.setValue(isChecked)
    }

    //确认密码显示开关
    var confirmPwdShowSwitchOnClickCommand: BindingCommand<Boolean> = BindingCommand { isChecked ->
        confirmPwdSwitchEvent.setValue(isChecked)
    }

    // 注册按钮
    val registerOnClickCommand = BindingCommand<Any>(IBindingAction {
        register()
    })

    fun setData(phone: String, verifyCode: String, type: Int) {
        phoneNumField.set(phone)
        verifyCodeField.set(verifyCode)
        this.type = type
    }

    private fun register() {

        if (TextUtils.isEmpty(pwdField.get())
            || TextUtils.isEmpty(confirmPwdField.get())) {
            ToastUtils.showShort("密码不能为空")
            return
        }

        if (pwdField.get().toString() != confirmPwdField.get().toString()) {
            ToastUtils.showShort("两次密码输入不一致，请重新输入！")
            return
        }

        when (type) {
            0 -> {
                HttpClient.instance.apiService
                    .register(phoneNumField.get().toString(),
                        confirmPwdField.get().toString(),
                        verifyCodeField.get().toString())
                    .doOnSubscribe { ui.showDialog() }
                    .compose(RxUtils.compose())
                    .subscribe(object : ApiResponse<Void>() {

                        override fun success(data: Void?, msg: String?) {
                            ui.hideDialog()
                            ToastUtils.showShort(msg)
                            RxBus.getDefault().post(EventInfo(AppConstant.EVENT_REGISTER_PWD_SUCCESS))
                        }

                        override fun onFail(e: Throwable?, errorMsg: String?) {
                            ui.hideDialog()
                            ToastUtils.showShort("$errorMsg")
                        }
                    })

            }
            1 -> {
                HttpClient.instance.apiService
                    .forgetPassword(phoneNumField.get().toString(),
                        confirmPwdField.get().toString(),
                        verifyCodeField.get().toString())
                    .doOnSubscribe { ui.showDialog() }
                    .compose(RxUtils.compose())
                    .subscribe(object : ApiResponse<TokenVO>() {

                        override fun success(data: TokenVO?, msg: String?) {
                            ui.hideDialog()
                            ToastUtils.showShort(msg)
                            RxBus.getDefault().post(EventInfo(AppConstant.EVENT_REGISTER_PWD_SUCCESS))
                        }

                        override fun onFail(e: Throwable?, errorMsg: String?) {
                            ui.hideDialog()
                            ToastUtils.showShort("$errorMsg")
                        }

                    })
            }
            else -> {

                HttpClient.instance.apiService
                    .modifyPassword(
                        confirmPwdField.get().toString(),
                        verifyCodeField.get().toString())
                    .doOnSubscribe { ui.showDialog() }
                    .compose(RxUtils.compose())
                    .subscribe(object : ApiResponse<TokenVO>() {

                        override fun success(data: TokenVO?, msg: String?) {
                            ui.hideDialog()
                            ToastUtils.showShort(msg)
                            RxBus.getDefault().post(EventInfo(AppConstant.EVENT_REGISTER_PWD_SUCCESS))
                        }

                        override fun onFail(e: Throwable?, errorMsg: String?) {
                            ui.hideDialog()
                            ToastUtils.showShort("$errorMsg")
                        }

                    })
            }
        }

    }
}